package com.ruoyi.business.service.impl;

import com.ruoyi.business.mapper.WsdSuggestionsMapper;
import com.ruoyi.business.service.IWsdSuggestionsService;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.dto.WsdSuggestionsDTO;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.entity.WsdCompany;
import com.ruoyi.common.core.domain.entity.WsdSuggestions;
import com.ruoyi.common.core.domain.vo.CompanyVO;
import com.ruoyi.common.core.domain.vo.WsdSuggestionsVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static com.ruoyi.common.utils.SecurityUtils.getLoginUser;

/**
 * 用于记录投诉和建议的信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-04-19
 */
@Service
public class WsdSuggestionsServiceImpl implements IWsdSuggestionsService
{
    @Autowired
    private WsdSuggestionsMapper wsdSuggestionsMapper;

    /**
     * 查询用于记录投诉和建议的信息
     * 
     * @param suggestionsId 用于记录投诉和建议的信息主键
     * @return 用于记录投诉和建议的信息
     */
    @Override
    public WsdSuggestionsVO selectWsdSuggestionsBySuggestionsId(Long suggestionsId) {
        // 从数据库查询出公司信息
        WsdSuggestions wsdSuggestions = wsdSuggestionsMapper.selectWsdSuggestionsBySuggestionsId(suggestionsId);
        // 创建一个新的CompanyVO对象，用于视图层展示
        WsdSuggestionsVO  wsdSuggestionsVO = new WsdSuggestionsVO();
        // 创建两个列表，用于存储分割后的图片和证书信息
        List<String> imagesList = new ArrayList<>();
        // 使用"||"作为分隔符分割图片信息字符串
        String[] images = wsdSuggestions.getImageUrl().split("\\|\\|");
        for (String image : images) {
            imagesList.add(image);
        }
        // 将从数据库查询的WsdCompany对象的属性复制到CompanyVO对象中
        BeanUtils.copyProperties(wsdSuggestions, wsdSuggestionsVO);
        // 设置CompanyVO对象的图片列表和证书列表
        wsdSuggestionsVO.setImageUrl(imagesList);
        // 返回CompanyVO对象
        return wsdSuggestionsVO;

    }

    /**
     * 查询用于记录投诉和建议的信息列表
     *
     * @param wsdSuggestions 用于记录投诉和建议的信息
     * @return 用于记录投诉和建议的信息
     */
    @Override
    public List<WsdSuggestions> selectWsdSuggestionsList(WsdSuggestions wsdSuggestions) {
        //根据当前学校查询投诉数据
        List<WsdSuggestions> wsdSuggestionsList = new ArrayList<>();
        SysUser user = getLoginUser().getUser();
        wsdSuggestions.setSchoolId(user.getSchoolId());
        Boolean inform = wsdSuggestions.getInform();
        //true表示未处理的投诉，false表示所有投诉,包含处理的
        if (Boolean.FALSE.equals(inform)){
            List<WsdSuggestions> list = wsdSuggestionsMapper.selectWsdSuggestionsList(wsdSuggestions);
            wsdSuggestionsList = list;
        }else {
            //未处理
            wsdSuggestions.setResolved(Constants.STATUSONE);
            List<WsdSuggestions> list = wsdSuggestionsMapper.selectWsdSuggestionsList(wsdSuggestions);
            wsdSuggestionsList = list;
        }
        return wsdSuggestionsList;
    }

    /**
     * 新增用于记录投诉和建议的信息
     * 
     * @param  wsdSuggestionsDTO 用于记录投诉和建议的信息
     * @return 结果
     */
    @Override
    public int insertWsdSuggestions(WsdSuggestionsDTO wsdSuggestionsDTO) {
        String imagesAsString = String.join("||", wsdSuggestionsDTO.getImageUrl());
        WsdSuggestions wsdSuggestions1 = new WsdSuggestions();
        BeanUtils.copyProperties(wsdSuggestionsDTO,wsdSuggestions1);
        wsdSuggestions1.setImageUrl(imagesAsString);
        return wsdSuggestionsMapper.insertWsdSuggestions(wsdSuggestions1);
    }

    /**
     * 回复记录投诉和建议的信息
     * 
     * @param wsdSuggestions 用于记录投诉和建议的信息
     * @return 结果
     */
    @Override
    public int updateWsdSuggestions(WsdSuggestions wsdSuggestions) {
        SysUser user = getLoginUser().getUser();
        String nickName = user.getNickName();
        wsdSuggestions.setResponderName(nickName);
        wsdSuggestions.setResponseDateTime(new Date());
        wsdSuggestions.setResolved(wsdSuggestions.getResolved());
        return wsdSuggestionsMapper.updateWsdSuggestions(wsdSuggestions);
    }

    /**
     * 批量删除用于记录投诉和建议的信息
     * 
     * @param suggestionsIds 需要删除的用于记录投诉和建议的信息主键
     * @return 结果
     */
    @Override
    public int deleteWsdSuggestionsBySuggestionsIds(Long[] suggestionsIds) {
        return wsdSuggestionsMapper.deleteWsdSuggestionsBySuggestionsIds(suggestionsIds);
    }

    /**
     * 删除用于记录投诉和建议的信息信息
     * 
     * @param suggestionsId 用于记录投诉和建议的信息主键
     * @return 结果
     */
    @Override
    public int deleteWsdSuggestionsBySuggestionsId(Long suggestionsId) {
        return wsdSuggestionsMapper.deleteWsdSuggestionsBySuggestionsId(suggestionsId);
    }

}
